
#include "stdafx.h"
#include "URLEncode.h"

CString URLEncode::char2hex(char dec)
{
    TCHAR dig1 = (dec&0xF0)>>4;
    TCHAR dig2 = (dec&0x0F);
    if ( 0<= dig1 && dig1<= 9) dig1+=48;    //0,48inascii
    if (10<= dig1 && dig1<=15) dig1+=97-10; //a,97inascii
    if ( 0<= dig2 && dig2<= 9) dig2+=48;
    if (10<= dig2 && dig2<=15) dig2+=97-10;

	CString r;
    r.Append( &dig1, 1);
    r.Append( &dig2, 1);
    return r;
}

CString URLEncode::encode(const CString &c)
{
	CString escaped = CString("");
	int max = c.GetLength();
    for(int i=0; i<max; i++)
    {
        if ( (48 <= c[i] && c[i] <= 57) ||	//0-9
             (65 <= c[i] && c[i] <= 90) ||	//abc...xyz
             (97 <= c[i] && c[i] <= 122) || //ABC...XYZ
             (c[i]=='~' || c[i]=='!' || c[i]=='*' || c[i]=='(' || c[i]==')' || c[i]=='\'') //~!*()'
			) 
        {
			CString s = CString(c[i]);
			escaped.Append(s, 1);
        } else {
			escaped.Append(_T("%"));
            escaped.Append(char2hex(c[i]));	//converts char 255 to string "ff"
        }
    }
    return escaped;
}

